from typing import *
from heapq import heappop, heappush, heapreplace


class Solution:
    def findXSum(self, nums: List[int], k: int, x: int) -> List[int]:
        n = len(nums)
        ans = []
        cnt = DefaultDict(int)
        for i, v in enumerate(nums):
            cnt[v] += 1
            if i >= k - 1:
                keys = sorted(cnt.items(), key=lambda item: (-item[1], -item[0]))
                sm = 0
                for j in range(min(x, len(keys))):
                    sm += keys[j][0] * keys[j][1]
                ans.append(sm)
                cnt[nums[i - k + 1]] -= 1

        return ans


s = Solution()
print(s.findXSum(nums=[1, 1, 2, 2, 3, 4, 2, 3], k=6, x=2))
